package 
{
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.utils.*;
	
	public class PhotoWalletModel extends EventDispatcher
	{
		public static const PHOTO_CHANGE:String = 'PHOTO_CHANGE';
		public static const XML_LOADED:String = 'XML_LOADED';
	
		private var _photoListURL:String;
		private var _photoList:XML;
		private var _loader:URLLoader;
		private var _currentPhoto:Number = 0;
		private var _slideShow:Boolean;
		private var _slideShowTimer:Timer;
		private var _slideShowInterval:int = 3000;
				
		public function PhotoWalletModel( photoListURL:String, slideShow:Boolean = false ) {
			
			setSlideShow( slideShow );
			setPhotoListURL( photoListURL );
			
		}
		
		// Creates a loader for the XML photo list and then loads the photo list
		private function loadPhotoList():void {
			
			_loader = new URLLoader();
			_loader.dataFormat = URLLoaderDataFormat.TEXT;
			_loader.addEventListener( Event.COMPLETE, handleXML );
			_loader.load( new URLRequest( _photoListURL ) );
			
		}
		
		// Once the XML file is loaded, proceed with the rest of the functionality
		private function handleXML( event:Event ):void {
			
			try {
				
				_photoList = new XML( event.target.data );
				dispatchEvent( new Event( PhotoWalletModel.XML_LOADED ) );
				setCurrentPhoto( _currentPhoto );
				
				if( _slideShow ) {
				
					startSlideShow();
				
				}
				
				
			} catch( e:TypeError ) {
				
				trace( 'Could not parse text into XML' );
				trace( e.message );
				
			}
			
		}
		
		public function startSlideShow():void {
			
			if( !( _slideShowTimer ) ) {
				
				_slideShowTimer = new Timer( _slideShowInterval );
				_slideShowTimer.addEventListener( TimerEvent.TIMER, goToNextSlide );
				
			}
			
			_slideShowTimer.start();
			setSlideShow( true );
			
		}
		
		public function stopSlideShow():void {
			
			_slideShowTimer.stop();
			setSlideShow( false );
			
		}
		
		private function goToNextSlide( e:Event ):void {
			
			goToNextPhoto();
			
		}
		
		public function goToNextPhoto():void {
			
			if( _currentPhoto != (_photoList.children().length() - 1) ) {
				setCurrentPhoto( ++_currentPhoto );
				
			} else {
				
				setCurrentPhoto( 0 );
				
			}
			
		}
		
		public function goToPreviousPhoto():void {
			
			if( _currentPhoto != 0 ) {
				
				setCurrentPhoto( --_currentPhoto );
				
			} else {
				
				setCurrentPhoto( _photoList.children().length() - 1 );
				
			}
			
		}
		
		public function setPhotoListURL( photoListURL:String ):void {
			
			_photoListURL = photoListURL;

			loadPhotoList();
			
		}
		
		public function getPhotoListURL():String {
			
			return _photoListURL;
			
		}
		
		private function setCurrentPhoto( index:Number ):void {
			
			_currentPhoto = index;
			
			dispatchEvent( new Event( PhotoWalletModel.PHOTO_CHANGE ) );
			
		}
		
		public function getCurrentPhoto():Number {
			
			return _currentPhoto;
			
		}
		
		public function getPhotoURL( currentPhoto:Number ):String {
			
			return _photoList..photo[ currentPhoto ];
			
		}
		
		public function getSlideShow():Boolean {
			
			return _slideShow;
			
		}
		
		public function setSlideShow( bool:Boolean = false ):void {
			
			_slideShow = bool;			
			
		}
		
		public function getPhotoListLength():Number {
			
			return _photoList.children().length();
			
		}
		
	}
}
